//
//  FKChooseBankcardVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/12.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKChooseBankcardVC.h"
#import "FKChooseBankCardCell.h"
#import "FKAddBankCardVC.h"
#import "FKBankCardListVC.h"

@interface FKChooseBankcardVC ()

@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKChooseBankcardVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择银行卡";
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem fk_itemWithImageName:@"card_add-" highImageName:@"card_add-" target:self action:@selector(addBankCard)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem fk_itemWithTarget:self action:@selector(bankcardManage) text:@"管理" textColor:fkColor333333 font:fk_adjustFont(16)];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.tableFooterView = [UIView new];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self loadData];
}

- (void)bankcardManage
{
    FKBankCardListVC *vc = [[FKBankCardListVC alloc] init];
    fkPushNav(vc);
}

//- (void)addBankCard
//{
//    FKAddBankCardVC *vc = [[FKAddBankCardVC alloc] init];
//    fkPushNav(vc);
//}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FKChooseBankCardCell *cell = [FKChooseBankCardCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(66);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectedBankCard) {
        NSDictionary *bankcard = self.datas[indexPath.row];
        self.selectedBankCard(bankcard);
        fkPopVC;
    }
}

- (void)loadData
{
    [SVProgressHUD show];
    [FKHttpTool postWithSuffix:@"m=Home&c=Bank&a=index" param:nil success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            [SVProgressHUD dismiss];
            self.datas = result.data[@"bank_info"];
            [self.tableView reloadData];
            
            if (self.bankCard) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    for (NSDictionary *bankcard in self.datas) {
                        if ([bankcard[@"id"] isEqualToString:self.bankCard[@"id"]]) {
                            NSInteger index = [self.datas indexOfObject:bankcard];
                            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:true scrollPosition:UITableViewScrollPositionNone];
                            break;
                        }
                    }
                });
            }
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

@end
